package com.macro.mall.portal.controller;

import com.macro.mall.portal.service.PmsPortalProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
@Controller
public class GoodsController {    @Resource
    @Autowired
    private PmsPortalProductService portalProductService;

    @PostMapping("/searchPic")
    public String searchPic(@RequestParam("file") MultipartFile multipartFile) throws IOException {


        *//*List<String> picNames = TestImageSearch.searchSimilar(multipartFile);

        return "mall/search";*//*
    }
    @GetMapping({"/search", "/search.html"})
    public String searchPage(@RequestParam Map<String, Object> params, HttpServletRequest request) {
        if (StringUtils.isEmpty(params.get("page"))) {
            params.put("page", 1);
        }
        params.put("limit", Constants.GOODS_SEARCH_PAGE_LIMIT);
        //封装分类数据
        if (params.containsKey("goodsCategoryId") && !StringUtils.isEmpty(params.get("goodsCategoryId") + "")) {
            Long categoryId = Long.valueOf(params.get("goodsCategoryId") + "");
            //获取所有3级分类（此项目中只用了第3级分类）
            SearchPageCategoryVO searchPageCategoryVO = newBeeMallCategoryService.getCategoriesForSearch(categoryId);
            if (searchPageCategoryVO != null) {
                request.setAttribute("goodsCategoryId", categoryId);
                request.setAttribute("searchPageCategoryVO", searchPageCategoryVO);
            }
        }


        //封装参数供前端回显
        if (params.containsKey("orderBy") && !StringUtils.isEmpty(params.get("orderBy") + "")) {
            request.setAttribute("orderBy", params.get("orderBy") + "");
        }
        //todo 根据所选分类或属性显示颜色和尺寸等其他属性(只做左侧导航栏用)
        PageQueryUtil propertiesPageUtil = new PageQueryUtil(params);
        String goodsCategoryId = String .valueOf(params.get("goodsCategoryId"));
        String colour = String.valueOf(params.get("goodsColour"));
        String goodsSize = String.valueOf(params.get("goodsSize"));
        params.get("goodsSize");
        List<NewBeeMallGoodsSku> selectedProperties = newBeeMallGoodsService.getSelectedProperties(propertiesPageUtil);
        request.setAttribute("selectedProperties",selectedProperties);

        String keyword = "";
        //对keyword做过滤 去掉空格
        if (params.containsKey("keyword") && !StringUtils.isEmpty((params.get("keyword") + "").trim())) {
            keyword = params.get("keyword") + "";
        }
        request.setAttribute("keyword", keyword);
        params.put("keyword", keyword);
        //搜索上架状态下的商品
        params.put("goodsSellStatus", Constants.SELL_STATUS_UP);
        //封装商品数据
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        //获取所有分类,展示到导航栏
        List<NewBeeMallIndexCategoryVO> categories = newBeeMallCategoryService.getCategoriesForIndex();
        if (CollectionUtils.isEmpty(categories)) {
            return "error/error_5xx";
        }
        request.setAttribute("categories", categories);//分类数据
        //获取所有尺寸,展示到导航栏
        List<String> allSkuSize = newBeeMallGoodsService.getAllSkuSize();
        if (CollectionUtils.isEmpty(allSkuSize)) {
            return "error/error_5xx";
        }
        request.setAttribute("allSkuSize",allSkuSize);
        //获取所有颜色,展示到导航栏
        List<String> allSkuColor = newBeeMallGoodsService.getAllSkuColor();
        if (CollectionUtils.isEmpty(allSkuColor)) {
            return "error/error_5xx";
        }
        request.setAttribute("allSkuColor",allSkuColor);
        //通用的根据分类，颜色，尺寸获取商品表的数据
        PageResult pageResult = newBeeMallGoodsService.searchNewBeeMallGoods(pageUtil);
        request.setAttribute("pageResult", pageResult);
        return "mall/search";
    }


    @GetMapping("/goods/detail/{goodsId}")
    public String detailPage(@PathVariable("goodsId") Long goodsId, HttpServletRequest request) {
        if (goodsId < 1) {
            return "error/error_5xx";
        }
        //根据id查询商品详情spu和sku
        NewBeeMallGoods goods = newBeeMallGoodsService.getNewBeeMallGoodsById(goodsId);
        if (goods == null) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_NOT_EXIST.getResult());
        }
        if (Constants.SELL_STATUS_UP != goods.getGoodsSellStatus()) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
        }
        //商品sku
        List<NewBeeMallGoodsSku> newBeeMallGoodsSku = newBeeMallGoodsService.getGoodsSkuByGoodsId(goodsId);
        Map<String,List<NewBeeMallGoodsSku>> skuColorMap = new HashMap<String,List<NewBeeMallGoodsSku>>();
        for (NewBeeMallGoodsSku sku : newBeeMallGoodsSku) {
            if (skuColorMap.containsKey(sku.getGoodsColor())) {
                skuColorMap.get(sku.getGoodsColor()).add(sku);
            }else {
                List<NewBeeMallGoodsSku> skuList = new ArrayList<NewBeeMallGoodsSku>();
                skuList.add(sku);
                skuColorMap.put(sku.getGoodsColor(),skuList);
            }
        }

        NewBeeMallGoodsDetailVO goodsDetailVO = new NewBeeMallGoodsDetailVO();
        BeanUtil.copyProperties(goods, goodsDetailVO);
        goodsDetailVO.setGoodsCarouselList(goods.getGoodsCarousel().split(","));
        //获取分类
        List<NewBeeMallIndexCategoryVO> categories = newBeeMallCategoryService.getCategoriesForIndex();
        if (CollectionUtils.isEmpty(categories)) {
            return "error/error_5xx";
        }
        request.setAttribute("categories", categories);//分类数据
        request.setAttribute("goodsDetail", goodsDetailVO);
        request.setAttribute("goodsSkus",newBeeMallGoodsSku);
        request.setAttribute("skuColorMap",skuColorMap);
        return "mall/detail";
    }

}*/
